package com.bite.redisDemo;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class RedisDemoHash {

    public static void test1(Jedis jedis){
        System.out.println("hset 和 hget");
        jedis.flushAll();

        jedis.hset("key1","f1","111");
        HashMap<String,String> fields = new HashMap<>();
        fields.put("f2","222");
        fields.put("f3","333");
        jedis.hset("key1",fields);

        String hgetResultF1 = jedis.hget("key1", "f1");
        String hgetResultF2 = jedis.hget("key1", "f2");
        System.out.println("hgetResultF1: " + hgetResultF1 + " hgetResultF2: " + hgetResultF2);

        String hgetResultF100 = jedis.hget("key1", "f100");
        System.out.println("hgetResultF100: " + hgetResultF100);

    }

    public static void test2(Jedis jedis){
        System.out.println("hexists");
        jedis.flushAll();

        jedis.hset("key1","f1","111");
        jedis.hset("key1","f2","222");
        jedis.hset("key1","f3","333");

        boolean hexistsF1 = jedis.hexists("key1", "f1");
        boolean hexistsF100 = jedis.hexists("key1", "f100");

        System.out.println("hexistsF1: " + hexistsF1 + " hexistsF100: " + hexistsF100);
    }

    public static void test3(Jedis jedis){
        System.out.println("hdel");
        jedis.flushAll();

        jedis.hset("key1","f1","111");
        HashMap<String,String> map = new HashMap<>();
        map.put("f2","222");
        map.put("f3","333");
        jedis.hset("key1",map);

        long hdelF1 = jedis.hdel("key1", "f1");
        long hdelF2F3 = jedis.hdel("key1", "f2","f3");
        long hdelF100 = jedis.hdel("key1", "f100");

        System.out.println("hdelF1: " + hdelF1);
        System.out.println("hdelF2F3: " + hdelF2F3);
        System.out.println("hdelF100: " + hdelF100);
    }

    public static void test4(Jedis jedis){
        System.out.println("hkeys 和 hvals");
        jedis.flushAll();

        jedis.hset("key1","f1","111");
        HashMap<String,String> map = new HashMap<>();
        map.put("f2","222");
        map.put("f3","333");
        jedis.hset("key1",map);

        Set<String> hkeysResult = jedis.hkeys("key1");
        List<String> hvalResult = jedis.hvals("key1");

        System.out.println("hkeysResult: " + hkeysResult);
        System.out.println("hvalResult: " + hvalResult);
    }

    public static void test5(Jedis jedis){
        System.out.println("hmget 和 hmset");
        jedis.flushAll();

        HashMap<String,String> map = new HashMap<>();
        map.put("f1","111");
        map.put("f2","222");
        map.put("f3","333");

        jedis.hmset("key1",map);
        List<String> hmgetResult = jedis.hmget("key1", "f1", "f2", "f3", "f4");
        System.out.println("hmgetResult: " + hmgetResult);

    }


    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);

        }catch (Exception e){
        }
    }
}
